/**
 * download file
 * @param {String} filename - filename, any illetal character will be replaced with underline
 * @param {String} text - file content 
 */
function download(filename, text) {
  const element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);
  element.style.display = 'none';
  document.body.appendChild(element);
  element.click();
  document.body.removeChild(element);
}


/**
 * 简单的 xhr get 请求
 * @param {String} url 
 * @return {Promise}
 */
function simpleGet(url) {
  return new Promise(function(resolve, reject) {
    const xhr = new XMLHttpRequest();
    xhr.onload = function() {
      resolve(this.responseText);
    };
    xhr.onabort = function() {
      reject(`reason=abort; url=${url}`);
    };
    xhr.onerror = function() {
      reject(`reason=error; url=${url}`);
    };
    xhr.ontimeout = function() {
      reject(`reason=timeout; url=${url}`);
    };
    xhr.open('get', url, true);
    xhr.send();
  });
}


function inlineSameOriginExternalStyles() {
  styles = Array.from(document.getElementsByTagName('link'))
    .filter(function(node) {
      return (node.rel == 'stylesheet' || node.type == 'text/css')
        && (new URL(node.href)).origin == location.origin;
    });
  return Promise.all(styles.map(function(node) {
    const url = node.href;
    return simpleGet(url)
      .then(function(value) {
        const c = document.createComment(url)
        node.parentNode.insertBefore(c, node);
        const n = document.createElement('style');
        n.type = 'text/css';
        n.innerHTML = value;
        node.parentNode.insertBefore(n, node);
        node.remove();
      }); 
  }));
}


function getHTML() {
  return '<!doctype html>\n<!-- ' + location.href + ' -->\n' + document.documentElement.outerHTML;
}


function normalizeURL(text) {
  // //... => http(s)://...
  text = text.replace(/(\s*(href|src)\s*=\s*['"])(\/\/[^/])/gi, `$1${location.protocol}$3`);
  // /a/b => http(s)://x.com/a/b
  text = text.replace(/(\s*(href|src)\s*=\s*['"])(\/[^/])/gi, `$1${location.origin}$3`);
  const baseURL = new URL('./', location.href).href;
  // ./a/b => http(s)://x.com/c/a/b
  text = text.replace(/(\s*(href|src)\s*=\s*['"])\.\/([^/])/gi, `$1${baseURL}$3`);
  // ../a/b => http(s)://x.com/c/../a/b
  text = text.replace(/(\s*(href|src)\s*=\s*['"])(\.\.\/[^/])/gi, `$1${baseURL}$3`);
  // a/b => http(s)://.../a/b
  text = text.replace(/(\s*(href|src)\s*=\s*['"])([^:./'"]+[/'"])/gi, `$1${baseURL}$3`);
  return text;
}


function omitScript(text) {
  return text.replace(/(<script (.|\n)+?<\/script>|<script>(.|\n)+?<\/script>)/gi, '<!-- script -->');
}


function omitIframeSrc(text) {
  return text.replace(/(<iframe [^>]*?src\s*?=\s*?['"]).+?(['"].+?<\/iframe>)/gi, '$1$2');
}

function sleep(ms){
  return new Promise((resolve)=>setTimeout(resolve,ms));
}

async function save(){
    await sleep(2000);
    var element = document.getElementById("mq");
    element.parentNode.removeChild(element);
    window.scrollTo(100,920)
    // $("body").append("<div id='yaotest'></div>");
    // await sleep(500);
    // document.getElementById('yaotest').click();
    // simulateClick();
    // await sleep(1000);
    // window.scrollTo(100,1400)
    // await sleep(1000);
    // window.scrollTo(100,2100)
    // await sleep(1000);
    // window.scrollTo(100,2800)
    // await sleep(1000);
    // window.scrollTo(100,3500)
    // await sleep(1000);
    // window.scrollTo(100,4200)
    // await sleep(1000);
    // window.scrollTo(100,4900)
    // await sleep(1000);
    // window.scrollTo(100,5600)
    // await sleep(1000);
    // window.scrollTo(100,6300)
    // await sleep(1000);
    // window.scrollTo(100,7000)
    // await sleep(1000);
    // window.scrollTo(100,7700)
    // await sleep(1000);
    // window.scrollTo(100,8400)
    // await sleep(1000);
    // window.scrollTo(100,9100)
    // await sleep(1000);
    // window.scrollTo(100,9800)
    // await sleep(1000);
    // window.scrollTo(100,10500)
    // await sleep(1000);
    // window.scrollTo(100,11200)
    // await sleep(1000);
    // window.scrollTo(100,1200)
    // await sleep(1000);
    // download(document.title + ".html", getHTML());
}



var simulateClick = function(){
 var evt = document.createEvent("MouseEvents");
 evt.initMouseEvent("click", true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
 document.body.dispatchEvent(evt);
}


// setTimeout(function(){console.log(getHTML()");}, 2000);
var patt=/detail.taobao.com|detail.tmall.com/i
// var patt=/taobao.com/i
if(patt.test(location.href)){
    save();
    // setTimeout(function(){
    //     // simulateClick();
    //     // $("body").append("<div id='yaotest'></div>");
    //     // document.getElementById('yaotest').click();
    //     // simulateClick();
    //     var element = document.getElementById("mq");
    //     element.parentNode.removeChild(element);

    // }, 2000);    
    // var div = document.createElement("div")
    // div.id = "yaotest"
    // document.appendChild(div);
    // document.getElementById('mq').blur();
    // $("input").blur();


    // save();
}


chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.cmd == 'getShapshotAsHtml') {
      inlineSameOriginExternalStyles()
            .then(function() {
                console.log('download...')
                text = getHTML();
                // filename = location.href + '-' + (new Date()).toISOString() + '.html';
                filename = document.title + '.html';
                var patt=/detail.taobao.com|detail.tmall.com/i
                if(patt.test(location.href)){
                    download(filename, text);
                    console.log('download')
                }
                sendResponse({
                    ok: true
                });
        })
        .onerror(function(reason) {
          console.log(`SNAPSHOT AS HTML ERROR: ${reason}`);
        });
    }
  }
);
